using System;

namespace FBroBrowserAutomation
{
    /// <summary>
    /// 微博热搜数据模型
    /// </summary>
    public class WeiboHotSearchItem
    {
        /// <summary>
        /// 排名
        /// </summary>
        public string Rank { get; set; }

        /// <summary>
        /// 热搜标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 热搜链接
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 热度值
        /// </summary>
        public string Heat { get; set; }

        /// <summary>
        /// 标签（热、新、沸等）
        /// </summary>
        public string Tag { get; set; }

        /// <summary>
        /// 图片链接
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// 采集时间
        /// </summary>
        public DateTime CollectTime { get; set; }

        public WeiboHotSearchItem()
        {
            CollectTime = DateTime.Now;
        }

        /// <summary>
        /// 转换为CSV行
        /// </summary>
        public string ToCsvLine()
        {
            return $"{EscapeCsv(Rank)},{EscapeCsv(Title)},{EscapeCsv(Url)},{EscapeCsv(Heat)},{EscapeCsv(Tag)},{EscapeCsv(ImageUrl)},{CollectTime:yyyy-MM-dd HH:mm:ss}";
        }

        /// <summary>
        /// CSV 字段转义
        /// </summary>
        private string EscapeCsv(string value)
        {
            if (string.IsNullOrEmpty(value))
                return "";

            // 如果包含逗号、引号或换行符，需要用引号包围并转义内部引号
            if (value.Contains(",") || value.Contains("\"") || value.Contains("\n"))
            {
                return "\"" + value.Replace("\"", "\"\"") + "\"";
            }

            return value;
        }

        /// <summary>
        /// CSV 表头
        /// </summary>
        public static string CsvHeader()
        {
            return "排名,标题,链接,热度值,标签,图片链接,采集时间";
        }
    }
}

